package com.loong.android.views;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.LayoutInflater;

import androidx.annotation.Nullable;

import com.loong.android.R;
import com.loong.android.views.utils.BitmapUtils;
import com.loong.android.views.utils.Unit;
import com.loong.android.views.utils.ViewTemplate;
import com.loong.android.views.utils.ViewUtils;

/**
 * 复选框
 */
public class LonCheckBox extends LonLinearLayout implements ViewTemplate {

    public LonCheckBox(Context context) {
        super(context);
        initAttrs(context, null);
    }

    public LonCheckBox(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initAttrs(context, attrs);
    }

    public LonCheckBox(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(context, attrs);
    }

    private Bitmap uncheckBitmap;
    private Bitmap checkedBitmap;
    private boolean checkedState = false;
    private int uncheckTextColor = 0xFF505050;
    private int checkedTextColor = 0xFF1195DB;

    private LonLinearLayout checkBoxRootView;
    private LonImageView checkBoxImgView;
    private LonTextView checkBoxLonTextView;

    private void initAttrs(Context context, AttributeSet attrs) {
        if (context == null) return;

        int vCheckBoxTextSize = ViewUtils.sp2px(context, 14);
        String vCheckBoxText = "CheckBox";

        if (attrs != null) {
            @SuppressLint("CustomViewStyleable")
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CheckBox);
            int checkMode = ta.getInt(R.styleable.CheckBox_checkMode, 0);
            //未选中图片
            int uncheckResId = ta.getResourceId(R.styleable.CheckBox_uncheckImg, -1);
            if(uncheckResId != -1) {
                uncheckBitmap = BitmapUtils.fromResource(context, uncheckResId);
            }else {
                if(checkMode == 0) {
                    uncheckBitmap = BitmapUtils.fromResource(context, R.drawable.ic_loong_widget_check_box1);
                }else {
                    uncheckBitmap = BitmapUtils.fromResource(context, R.drawable.ic_loong_widget_radio1);
                }
            }
            //选中图片
            int checkedResId = ta.getResourceId(R.styleable.CheckBox_checkedImg, -1);
            if(checkedResId != -1) {
                checkedBitmap = BitmapUtils.fromResource(context, checkedResId);
            }else {
                if(checkMode == 0) {
                    checkedBitmap = BitmapUtils.fromResource(context, R.drawable.ic_loong_widget_check_box2);
                }else {
                    checkedBitmap = BitmapUtils.fromResource(context, R.drawable.ic_loong_widget_radio2);
                }
            }

            checkedState = ta.getBoolean(R.styleable.CheckBox_checkBoxState, checkedState);

            uncheckTextColor = ta.getColor(R.styleable.CheckBox_uncheckTextColor, uncheckTextColor);
            checkedTextColor = ta.getColor(R.styleable.CheckBox_checkedTextColor, checkedTextColor);

            vCheckBoxTextSize = ta.getDimensionPixelSize(R.styleable.CheckBox_checkBoxTextSize, vCheckBoxTextSize);
            vCheckBoxText = ta.getString(R.styleable.CheckBox_checkBoxText);

            ta.recycle();
        }

        initView(context, vCheckBoxText, vCheckBoxTextSize);
    }

    private void initView(Context context, String text, int textSize) {
        android.view.View view = LayoutInflater.from(context).inflate(R.layout.loong_widget_check_box_view, this, true);
        checkBoxRootView = view.findViewById(R.id.view_root);
        checkBoxImgView = view.findViewById(R.id.img);
        checkBoxLonTextView = view.findViewById(R.id.tv);
        checkBoxLonTextView.text(text);
        checkBoxLonTextView.textSize(Unit.PX, textSize);
        redraw();
        initEvent();
    }

    private void redraw() {
        checkBoxImgView.src(checkedState ? checkedBitmap : uncheckBitmap);
        checkBoxLonTextView.textColor(checkedState ? checkedTextColor : uncheckTextColor);
    }

    //region 自定义的方法

    public LonLinearLayout getCheckBoxRootView() {
        return checkBoxRootView;
    }

    public LonImageView getCheckBoxImgView() {
        return checkBoxImgView;
    }

    public LonTextView getCheckBoxTextView() {
        return checkBoxLonTextView;
    }

    public void setCheckedTextColor(int color) {
        this.checkedTextColor = color;
        if(checkedState) checkBoxLonTextView.textColor(checkedTextColor);
    }

    public int getCheckedTextColor() {
        return checkedTextColor;
    }

    public void setUncheckTextColor(int color) {
        this.uncheckTextColor = color;
        if(!checkedState) checkBoxLonTextView.textColor(uncheckTextColor);
    }

    public int getUncheckTextColor() {
        return uncheckTextColor;
    }

    public void setCheckedState(boolean checkedState) {
        if(this.checkedState == checkedState) return;
        this.checkedState = checkedState;
        redraw();
    }

    public boolean getCheckedState() {
        return checkedState;
    }

    public void setCheckedImg(int resId) {
        checkedBitmap = BitmapUtils.fromResource(getContext(), resId);
        if(checkedState) checkBoxImgView.src(checkedBitmap);
    }

    public void setCheckedImg(Bitmap bitmap) {
        checkedBitmap = bitmap;
        if(checkedState) checkBoxImgView.src(checkedBitmap);
    }

    public void setCheckedImg(String filePath) {
        checkedBitmap = BitmapUtils.fromFile(filePath);
        if(checkedState) checkBoxImgView.src(checkedBitmap);
    }

    public void setCheckedImg(byte[] bytes) {
        checkedBitmap = BitmapUtils.fromBytes(bytes);
        if(checkedState) checkBoxImgView.src(checkedBitmap);
    }

    public Bitmap getCheckedBitmap() {
        return checkedBitmap;
    }

    public void setUncheckImg(int resId) {
        uncheckBitmap = BitmapUtils.fromResource(getContext(), resId);
        if(!checkedState) checkBoxImgView.src(uncheckBitmap);
    }

    public void setUncheckImg(Bitmap bitmap) {
        uncheckBitmap = bitmap;
        if(!checkedState) checkBoxImgView.src(uncheckBitmap);
    }

    public void setUncheckImg(String filePath) {
        uncheckBitmap = BitmapUtils.fromFile(filePath);
        if(!checkedState) checkBoxImgView.src(uncheckBitmap);
    }

    public void setUncheckImg(byte[] bytes) {
        uncheckBitmap = BitmapUtils.fromBytes(bytes);
        if(!checkedState) checkBoxImgView.src(uncheckBitmap);
    }

    public Bitmap getUncheckBitmap() {
        return uncheckBitmap;
    }

    /**
     * 取复选框
     * @param text
     */
    public void setText(String text) {
        checkBoxLonTextView.text(text);
    }

    /**
     * 取选择框文本
     */
    public String getText() {
        return checkBoxLonTextView.text();
    }

    private void initEvent() {
        checkBoxRootView.setOnClick(new LonView.ClickCallback() {
            @Override
            public void onClick(android.view.View view) {
                super.onClick(view);
                checkedState = !checkedState;
                redraw();
                if(mCheckEvent != null) mCheckEvent.onCheckStateChange(checkedState);
            }
        });
    }

    private CheckEvent mCheckEvent;

    /**
     * 设置选中事件
     * @param event 事件回调
     */
    public void setCheckEvent(CheckEvent event) {
        this.mCheckEvent = event;
    }

    /**
     * 选中事件
     */
    public abstract static class CheckEvent {
        /**
         * 选中状态改变
         * @param checkState 选中状态
         */
        public void onCheckStateChange(boolean checkState) {}
    }
}
